#include "io_out.h"
#include <bl_gpio.h>
#include "buzzer.h"
#include "light.h"

typedef struct {
    uint8_t pin;
    uint8_t active_low;
    uint8_t pull_up;
} io_t;

io_t io_devs[] = {
    [BUZZER] = {
        .pin = 5,
        .active_low = 1,
        .pull_up = 1,
    },
    [RELAY_1] = {
        .pin = 14,
        .active_low = 0,
        .pull_up = 1,
    },
    [RELAY_2] = {
        .pin = 12,
        .active_low = 0,
        .pull_up = 1,
    },
    [LIGHT] = {
        .pin = 3,
        .active_low = 0,
        .pull_up = 0,
    },
    [LED] = {
        .pin = 17,
        .active_low = 0,
        .pull_up = 0,
    },
};

void io_out(channel_t channel, uint8_t value)
{
    switch (channel) {
        case BUZZER: buzzer_out(value); break;
        case LIGHT: light_out(value); break;
        default: bl_gpio_output_set(io_devs[channel].pin, value ^ io_devs[channel].active_low);
    }
    // if (channel == BUZZER) {
    //     buzzer_out(value);
    // } else {
    //     bl_gpio_output_set(io_devs[channel].pin, value ^ io_devs[channel].active_low);
    // }
}

void init_io(void)
{
    for (channel_t i = 0; i < _CHANNEL_MAX; i++) {
        bl_gpio_enable_output(io_devs[i].pin, io_devs[i].pull_up, !io_devs[i].pull_up);
        io_out(i, 0);
    }
}
